/*
 * @Descripttion: 用于创建模板
 * @version: 
 * @Author: Ybb
 * @Date: 2022-10-11 15:46:40
 * @LastEditors: Andy
 * @LastEditTime: 2022-10-13 19:48:09
 */
const comName = require('./components.js')
module.exports = function(plop) {
  plop.setGenerator('controller', {
    description: 'application controller logic',
    prompts: [{
      type: 'input',
      name: 'realName',
      message: '请输入组件中文名称'
    },
    {
      type: 'input',
      name: 'name',
      message: '请输入组件名称'
    }],
    actions(data) {
      let actions = []
      // 去重
      if (comName && Object.keys(comName).length > 0) {
        const found = Object.keys(comName).includes(data.name)
        if (found) {
          console.error(data.name + '组件已存在')
          return actions
        }
      }
      actions = actions.concat([
        {
          type: 'add',
          path: 'packages/components/{{name}}/index.vue',
          templateFile: 'build/plop-templates/main.hbs'
        },
        {
          type: 'add',
          path: 'packages/components/{{name}}/index.js',
          templateFile: 'build/plop-templates/entry.hbs'
        },
        {
          type: 'add',
          path: 'examples/docs/{{name}}.md',
          templateFile: 'build/plop-templates/doc.hbs'
        },
        {
          type: 'append',
          path: 'components.js',
          pattern: "/** plop component */",
          template: "{{name}}: './packages/components/{{name}}/index.js',"
        }
      ])
      return actions
    }
  })
}